package com.novunda.platform.common.dialects.shiro.processor;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.thymeleaf.IEngineConfiguration;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.exceptions.TemplateProcessingException;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.processor.element.AbstractElementTagProcessor;
import org.thymeleaf.processor.element.IElementTagStructureHandler;
import org.thymeleaf.standard.expression.IStandardExpression;
import org.thymeleaf.standard.expression.IStandardExpressionParser;
import org.thymeleaf.standard.expression.StandardExpressions;
import org.thymeleaf.templatemode.TemplateMode;

/**
 * @author linfeng
 * @since 2016/9/12
 */
public abstract class AbstractElementProcessor extends AbstractElementTagProcessor {

    private static final Logger log = LoggerFactory.getLogger(AbstractElementProcessor.class);

    protected AbstractElementProcessor(TemplateMode templateMode, String dialectPrefix,
                                       String elementName, boolean prefixElementName,
                                       String attributeName, boolean prefixAttributeName, int precedence) {
        super(templateMode, dialectPrefix, elementName, prefixElementName, attributeName, prefixAttributeName, precedence);
    }

    @Override
    protected void doProcess(ITemplateContext context, IProcessableElementTag tag, IElementTagStructureHandler structureHandler) {



        /**
         * 获得name属性的具体值
         */
        final String nameValue = tag.getAttributeValue("name");

        if (StringUtils.isBlank(nameValue)) {
            structureHandler.removeElement();
        } else {

            String permission;

            try {
                final IEngineConfiguration configuration = context.getConfiguration();

                /**
                 * 获得thymeleaf标准表达式解析器
                 */
                final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);

                /**
                 * 解析属性值为thymeleaf的规范表达式
                 */
                final IStandardExpression nameExpression = parser.parseExpression(context, nameValue);

                /**
                 * 执行刚刚解析的表达式
                 */
                permission = (String) nameExpression.execute(context);
            } catch (TemplateProcessingException e) {

                if (log.isWarnEnabled()) {
                    log.warn(e.getMessage());
                }
                permission = nameValue;
            }

            doShiroHandler(context, tag, permission, structureHandler);
        }


    }

    protected abstract void doShiroHandler(ITemplateContext context, IProcessableElementTag tag,
                                           String permissionValue, IElementTagStructureHandler structureHandler);
}
